using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesAdminCompanyDetail : System.Web.UI.Page
{
    #region Variables

    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected GonzalesCompany CurCompany = null;

    #region CompanyID
    protected string CompanyID
    {
        get
        {
            return ViewState["CompanyID"].ToString();
        }

        set
        {
            ViewState["CompanyID"] = value;
        }
    }
    #endregion

    #region warningMessage
    protected string warningMessage
    {
        get
        {
            return ViewState["warningMessage"].ToString();
        }

        set
        {
            ViewState["warningMessage"] = value;
        }
    }
    #endregion

    #region ConfirmedMessage
    protected string ConfirmedMessage
    {
        get
        {
            return ViewState["ConfirmedMessage"].ToString();
        }

        set
        {
            ViewState["ConfirmedMessage"] = value;
        }
    }
    #endregion

    #region ErrorMessage
    protected string ErrorMessage
    {
        get
        {
            return ViewState["ErrorMessage"].ToString();
        }

        set
        {
            ViewState["ErrorMessage"] = value;
        }
    }
    #endregion

    #endregion

    #region Events

    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        if (!IsPostBack)
        {
           
            #region Request.QueryString["id"]

            if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36)
            {
                this.CompanyID = Request.QueryString["id"];
            }
            else
            {
                Response.Redirect(GonzalesErrorPageRedirect.PageFlowError);
            }

            #endregion

            this.SwitchLanguage();
            this.PageComponentSetup();
            this.SwitchLanguage();

            this.LoadData();
            this.DataFill();

            // log down the history
            this.CurLogManager.LogMessage("[GonzalesAdminCompanyDetail] : Company detail (" + this.CompanyID + ", " + this.LabelValue_CompanyCode.Text + ") viewed", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);

        }
    }

    protected void Button_Submit_Click(object sender, EventArgs e)
    {
        if (this.DataValidate())
        {
            this.DataCollect();

            if (this.DataSave())
            {
                // log down the history
                this.CurLogManager.LogMessage("[GonzalesAdminCompanyDetail.Button_Submit_Click] : Company detail (" + this.CompanyID + ", " + this.LabelValue_CompanyCode.Text + ") updated", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);

                Response.Redirect("GonzalesAdminCompanyListing.aspx");
            }
        }
    }

    protected void ImageButton_Cancel_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("GonzalesAdminCompanyListing.aspx");
    }

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        // credit card payment type
        this.DropDownList_CCPaymentType.Items.Add(new ListItem(CreditCardManager.GetCreditCardPaymentTypeInString(TypeOfCreditCardPayment.PaidByCompany), CreditCardManager.GetCreditCardPaymentTypeInCode(TypeOfCreditCardPayment.PaidByCompany).ToString()));
        this.DropDownList_CCPaymentType.Items.Add(new ListItem(CreditCardManager.GetCreditCardPaymentTypeInString(TypeOfCreditCardPayment.PaidByEmployee), CreditCardManager.GetCreditCardPaymentTypeInCode(TypeOfCreditCardPayment.PaidByEmployee).ToString()));

        this.ImageButton_Cancel.Attributes["onclick"] = "return confirm('"+ this.warningMessage +"');";
        this.Button_Submit.Attributes["onclick"] = "return confirm('"+ this.ConfirmedMessage +"');";
        
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_CompanyID.Text = CurLanguageFactory.DisplayValue("CompanyID");
                this.Label_CreditCardCutOff.Text = CurLanguageFactory.DisplayValue("CreditCardCutOff");
                this.Label_CreditCardOutputDay.Text = CurLanguageFactory.DisplayValue("CreditCardOutputDay");
                this.Label_CreditCardPaymentType.Text = CurLanguageFactory.DisplayValue("CreditCardPaymentType");
                this.Label_DefaultLDAP.Text = CurLanguageFactory.DisplayValue("DefaultLDAP");
                this.Label_ExpiryDate.Text = CurLanguageFactory.DisplayValue("ExpiryDate");
                this.Label_ExpiryDateInfo.Text = CurLanguageFactory.DisplayValue("ExpiryDateInfo");
                this.Label_FileOutDay.Text = CurLanguageFactory.DisplayValue("FileOutDay");
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_IsRepresentative.Text = CurLanguageFactory.DisplayValue("IsRepresentative");
                this.Label_MileageRate.Text = CurLanguageFactory.DisplayValue("MileageRate");
                this.Label_MileageRate2.Text = CurLanguageFactory.DisplayValue("MileageRate2");
                this.Label_Name.Text = CurLanguageFactory.DisplayValue("Name");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Label_PaymentDateRemarks.Text = CurLanguageFactory.DisplayValue("PaymentDateRemarks");
                this.Label_PettyCashLimit.Text = CurLanguageFactory.DisplayValue("PettyCashLimit");
                this.Label_UsingVAT.Text = CurLanguageFactory.DisplayValue("UsingVAT");
                this.Label1.Text = CurLanguageFactory.DisplayValue("Label1");
                this.Label2.Text = CurLanguageFactory.DisplayValue("Label2");
                this.Label3.Text = CurLanguageFactory.DisplayValue("Label3");
                this.Label4.Text = CurLanguageFactory.DisplayValue("Label4");
                this.Button_Submit.Text = CurLanguageFactory.DisplayValue("Submit");
                this.ErrorMessage = CurLanguageFactory.DisplayValue("ErrorMessage");
                this.warningMessage = CurLanguageFactory.DisplayValue("warningMessage");
                this.ConfirmedMessage = CurLanguageFactory.DisplayValue("ConfirmedMessage");

            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesAdminAuthorizedDetail.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region ShowErrorMessage

    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
    }

    protected void ShowErrorMessage(ArrayList ErrorMessageList)
    {
        if (ErrorMessageList != null)
        {
            StringBuilder ErrorMessageBuilder = new StringBuilder("<ul>");

            for (int x = 0; x < ErrorMessageList.Count; x++)
            {
                ErrorMessageBuilder.AppendLine("<li>" + ErrorMessageList[x] + "</li>");
            }

            ErrorMessageBuilder.AppendLine("</ul>");

            this.Table_MessageBox.Visible = true;
            this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessageBuilder.ToString();
        }
    }

    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {            
            using (CompanyManager CurCompanyManager = new CompanyManager(this.CurrentPrivateSession))
            {
                this.CurCompany = CurCompanyManager.GetCompanyByID(this.CompanyID);

                if (CurCompany == null)
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesAdminCompanyDetail.LoadData] : Failed at CurCompanyManager.GetCompanyByID('" + this.CompanyID + "') : " + CurCompanyManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                    this.ShowErrorMessage(this.ErrorMessage + LogID);
                }

                int[] FileOutputDays = CurCompanyManager.GetPaymentDayByCompany(this.CompanyID);
                if (FileOutputDays != null && FileOutputDays.Length > 0)
                {
                    for (int x = 0; x < FileOutputDays.Length; x++)
                    {
                        this.TextBox_FileOutpurDay.Text += FileOutputDays[x] + ",";
                    }

                    this.TextBox_FileOutpurDay.Text = this.TextBox_FileOutpurDay.Text.Substring(0, (this.TextBox_FileOutpurDay.Text.Length - 1));
                }
            }
    }

    #endregion

    #region DataFill

    protected void DataFill()
    {
        if (this.CurCompany != null)
        {
            this.LabelValue_CompanyCode.Text = CurCompany.CompanyCode;
            this.TextBox_CompanyName.Text = CurCompany.CompanyName;

            this.CheckBox_IsRep.Checked = CurCompany.IsRepresentative;
            this.DropDownList_CCPaymentType.Items.FindByValue(CreditCardManager.GetCreditCardPaymentTypeInCode(CurCompany.CreditCardPaymentType).ToString()).Selected = true;
            this.TextBox_MileageRate.Text = CurCompany.MileageRate.ToString();
            this.TextBox_PettyCashLimit.Text = CurCompany.PettyCashLimit.ToString();
            this.TextBox_DefaultLDAP.Text = CurCompany.DefaultLDAP;
            this.TextBox_CCCutOff.Text = CurCompany.CreditCardCutOffDay.ToString();
            this.TextBox_CCOutputDays.Text = CurCompany.CreditCardActivatedDay.ToString();
            this.CheckBox_VAT.Checked = CurCompany.UsingVAT;
            this.TextBox_ExpiryDate.Text = CurCompany.ExpiryDate;

            //Added By Agbes[31March2009] - for enhancement
            this.TextBox_ClaimsReminder.Text = CurCompany.ClaimReminder;
            this.TextBox_CreditCardReminder.Text = CurCompany.CreditCardReminder;

            /*
            if (CurCompany.CompanyManager != null)
            {
                this.LabelValue_Manager.Text = CurCompany.CompanyManager.DisplayName;
                this.HiddenField_CompanyManager.Value = CurCompany.CompanyManager.InternalID;
            }
            */
        }
    }

    #endregion

    #region DataValidate
    protected bool DataValidate()
    {
        bool smooth = true;



        return smooth;
    }
    #endregion

    #region DataCollect

    protected void DataCollect()
    {
        this.CurCompany = new GonzalesCompany(this.CompanyID, this.TextBox_CompanyName.Text);
        this.CurCompany.IsRepresentative = this.CheckBox_IsRep.Checked;
        this.CurCompany.CompanyManager = new SystemUser("8841297A-D24D-4EA9-A690-16447FD93502", "8841297A-D24D-4EA9-A690-16447FD93502"); //("94DBBBD3-AC24-40D2-BEDE-84C00ED709D1", "94DBBBD3-AC24-40D2-BEDE-84C00ED709D1");
        this.CurCompany.CreditCardPaymentType = CreditCardManager.GetCreditCardPaymentTypeFromCode(Int32.Parse(this.DropDownList_CCPaymentType.SelectedValue));
        this.CurCompany.MileageRate = decimal.Parse(this.TextBox_MileageRate.Text);
        this.CurCompany.PettyCashLimit = decimal.Parse(this.TextBox_PettyCashLimit.Text);
        this.CurCompany.DefaultLDAP = this.TextBox_DefaultLDAP.Text;
        this.CurCompany.CreditCardActivatedDay = Int32.Parse(this.TextBox_CCOutputDays.Text);
        this.CurCompany.CreditCardCutOffDay = Int32.Parse(this.TextBox_CCCutOff.Text);
        this.CurCompany.UsingVAT = this.CheckBox_VAT.Checked;
        this.CurCompany.ExpiryDate = this.TextBox_ExpiryDate.Text;
        this.CurCompany.CreditCardReminder = this.TextBox_CreditCardReminder.Text;
        this.CurCompany.ClaimReminder = this.TextBox_ClaimsReminder.Text;
    }

    #endregion

    #region DataSave

    protected bool DataSave()
    {
        bool smooth = true;

        using (CompanyManager CurCompanyManager = new CompanyManager(this.CurrentPrivateSession))
        {
            if (!(smooth = CurCompanyManager.UpdateCompany(this.CurCompany)))
            {
                string LogID = SwissArmy.UniqueID();
                this.CurLogManager.LogError(LogID, "[GonzalesAdminCompanyDetail.DataSave] : Failed at CurCompanyManager.UpdateCompany() : " + CurCompanyManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                this.ShowErrorMessage(this.ErrorMessage + LogID);
            }

            // update payment day
            try
            {
                int[] FileOutputDays = null;
                if (this.TextBox_FileOutpurDay.Text.Length > 0)
                {
                    string[] FileOutputDaysInStr = this.TextBox_FileOutpurDay.Text.Split(',');

                    if (FileOutputDaysInStr.Length > 0)
                    {
                        FileOutputDays = new int[FileOutputDaysInStr.Length];

                        for (int x = 0; x < FileOutputDaysInStr.Length; x++)
                        {
                            FileOutputDays[x] = Int32.Parse(FileOutputDaysInStr[x].Trim());
                        }
                    }
                }

                if (!(smooth = CurCompanyManager.UpdatePaymentDay(FileOutputDays, this.CompanyID)))
                {
                    string LogID = SwissArmy.UniqueID();
                    this.CurLogManager.LogError(LogID, "[GonzalesAdminCompanyDetail.DataSave] : Failed at CurCompanyManager.UpdatePaymentDay() : " + CurCompanyManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                    this.ShowErrorMessage(this.ErrorMessage + LogID);
                }
            }
            catch (Exception ex)
            {
                string LogID = SwissArmy.UniqueID();
                this.CurLogManager.LogError(LogID, "[GonzalesAdminCompanyDetail.DataSave] : Failed at Update Payment dAY : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                this.ShowErrorMessage(this.ErrorMessage + LogID);
            }
        }

        return smooth;

    }

    #endregion

    #endregion

}
